﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLocalChurch.Business;
using MyLocalChurch.Constants;
using MyLocalChurch.Models;
using MyLocalChurch.Models.Enumerations;
using MyLocalChurch.Models.KidsChurch;
using MyLocalChurch.Models.Resources;
using MyLocalChurch.Resources;
using RKConsulting.Helpers;
using RKConsulting.Web.Helpers;

namespace MyLocalChurch.KidsChurch
{
    public partial class CreateKid : System.Web.UI.Page
    {
        private readonly MyContext _context = MyContextSingleton.Instance;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            var guardianId = QueryHelper.GetIntQueryParam(QueryParameters.GuardianId);
            var guardian = GuardianManager.GetGuardianAndUpdateCache(_context, guardianId);
            lblGuardianName.Text = guardian.ToString();

            ddlGender.BindEnumValues<Gender>();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                Session[ImageHandler.SessionName] = null;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (!Page.IsValid)
                return;

            var guardianId = QueryHelper.GetIntQueryParam(QueryParameters.GuardianId);
            var guardian = GuardianManager.GetGuardianAndUpdateCache(_context, guardianId);
            var securityCode = guardian.GetSecurityCode();

            var kid = new Kid
                {
                    FirstName = txtFirstName.Text,
                    MiddleName = txtMiddleName.Text,
                    LastName = txtLastName.Text,
                    NickName = txtNickName.Text,
                    Gender = (int)EnumHelper.Parse<Gender>(ddlGender.SelectedValue),
                    BirthDate = dpBirthDate.SelectedDate.HasValue
                                    ? dpBirthDate.SelectedDate.Value
                                    : default(DateTime),
                    Allergies = txtAllergies.Text,
                    RelationshipWithGuardian = txtRelationshipWithGuardian.Text,
                    AdditionalInformation = txtAdditionalInformation.Text,
                    Photo = PhotoManager.CreatePhotoFromFileUploadOrSession(fileUploadPhoto, HttpContext.Current),
                    CreatedBy = SystemConstants.User,
                    CreatedDate = DateTime.Now
                };

            kid.Guardians.Add(guardian);
            kid.Barcodes.Add(BarcodeManager.GenerateBarcode(_context, securityCode));

            guardian.Kids.Add(kid);
            _context.Kids.Add(kid);
            _context.SaveChanges();

            Response.Redirect(string.Concat("KidsList.aspx?", QueryParameters.GuardianId, "=", guardianId));
        }

        protected void RefreshKidCategory()
        {
            if (!dpBirthDate.SelectedDate.HasValue)
            {
                lblKidCategory.Text = string.Empty;
                return;
            }

            lblKidCategory.Text = KidManager.GetKidCategory(dpBirthDate.SelectedDate.Value).ToString();
        }

        protected void txtBirthDate_TextChanged(object sender, EventArgs e)
        {
            RefreshKidCategory();
        }

        protected void btnPhotoPreview_Click(object sender, EventArgs e)
        {
            Session[ImageHandler.SessionName] = fileUploadPhoto.FileBytes;
            var imageHandlerUrl = ConfigurationManager.AppSettings[AppSettingKeys.ImageHandlerUrl];
            imagePreview.ImageUrl = imageHandlerUrl;
        }
    }
}